/**
  ******************************************************************************
  * @file    DPC_Miscellaneous.h
  * @author  STMicroelectronics
  * @date    09-June-2022
  * @brief   This file contains type definitions for LED Control
  ******************************************************************************
  *
  * @copyright (c) 2021(-2022) Biricha Digital Power Limited
  * @copyright (c) 2022(-2023) STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file in
  * the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  ******************************************************************************
  *
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MISC_TYPE_H
#define __MISC_TYPE_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#ifdef STM32F334x8
  #include "stm32f3xx_hal.h" 
#elif defined STM32G474xx
  #include "stm32g4xx_hal.h" 
#else
  #error CPU  not specified
#endif


/* Private includes ----------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/**
  * @brief enum type definition, it lists all possible LED's modality
  */
typedef enum
{
  LED_OFF = 0,          /* LED off until new command */
  LED_ON,               /* LED on until new command */
  LED_BLINK_N,          /* LED blinks n times  */
  LED_BLINK_INF         /* LED blinks until new command */
} LED_Modality_t;

/**
  * @brief struct type definition, it contains all parameters regarding LED's blinking and state
  */
typedef struct
{
  GPIO_TypeDef*    GPIOx;                    /* GPIO peripheral port selection to address LED */
  uint16_t         GPIO_Pin;                 /* Port bit to be written adressing LED */
  LED_Modality_t   LED_Modality;             /* LED modality, it can be a value of LED_Modality_t type */
  uint8_t          nBlinkCount;              /* total number of LED blinks */
  uint8_t          nBlinkLeftCount;          /* remaining number of LED blinks */
  bool             bActualOn;                /* TRUE if LED is currently on, FALSE otherwise */
  uint16_t         hBlinkPeriodms;           /* blink period in ms */
  uint16_t         hBlinkPeriodCounterLeft;  /* remaining period counter in ms */
  uint16_t         hBlinkRepetitionPeriodms; /* period between a burst of blinks and the next */
} LED_Status_t;

/**
  * @brief enum type definition, it lists all possible button's modality
  */
typedef enum 
{
  KEY_RELEASED = 0,
  KEY_PUSHED,
  KEY_PUSHED_PROCESSED,
  KEY_REPEAT,
  KEY_REPEAT_PROCESSED,
  KEY_TRIGGER,
  KEY_SHUTDOWN,
  KEY_SHUTDOWN_PROCESSED
} SystStatus_key;

/** 
  * @brief struct type definition, it contains all parameters to manage user's button
  */
typedef struct KEY_DEBOUNCE_t
{
  GPIO_TypeDef*    GPIOx;                    /* GPIO peripheral port selection to address KEY */
  uint16_t         GPIO_Pin;                 /* Port bit to be written adressing KEY */
//  SystStatus_key   KEY_Modality;             /* KEY modality, it can be a value of SystStatus_key type */
  uint8_t          m_State;                  /* FALSE when KEY has just been pressed, TRUE otherwise */
} KEY_DEBOUNCE_t;


extern SystStatus_key Key_USER_State;


/* Exported macro ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
#define U8_MAX     ((uint8_t)255)
#define S8_MAX     ((int8_t)127)
#define S8_MIN     ((int8_t)-127)
#define U16_MAX    ((uint16_t)65535u)
#define S16_MAX    ((int16_t)32767)
#define S16_MIN    ((int16_t)-32767)
#define U32_MAX    ((uint32_t)4294967295uL)
#define S32_MAX    ((int32_t)2147483647)
#define S32_MIN    ((int32_t)-2147483647)
    
    
/* Private defines -----------------------------------------------------------*/
/* Exported functions prototypes ---------------------------------------------*/
void UserLEDOn(LED_Status_t* pLED);
void UserLEDOff(LED_Status_t* pLED);
void UserLEDToggle(LED_Status_t* pLED);
void LED_SetParams(LED_Status_t* pLED, LED_Modality_t newLEDModality, uint8_t nBlinkCounter, uint16_t hBlinkPeriodms, uint16_t hhBlinkRepetitionPeriodms);
void LED_DecreaseBlinkCounter(LED_Status_t* pLED);
void LED_Task(LED_Status_t* pLED);
bool KeyDebounce(KEY_DEBOUNCE_t *pKEY);


/* Private variables */
/* Private defines -----------------------------------------------------------*/

#ifdef __cplusplus
}
#endif

#endif /* __MISC_TYPE_H */